import { dateFormat } from "@/basa/utils";
import store from "../../config/store";
import { addUrlPerFix } from "./index";
// import router from "../router";
export default {
  install: (Vue, options) => {
    Vue.prototype.$addUrlPerFix = function(url) {
      return addUrlPerFix(url);
    };
    Vue.prototype.orderStatisticUrl = "/statistic/order/subcontract";

    Vue.prototype.tableHeightWatch = function(
      _this,
      wrapperDom,
      tableWrapperDom
    ) {
      _this.$nextTick(() => {
        // 获取表格所占区域的总高度
        _this.elCardBodyHeight = _this.$refs[wrapperDom].querySelector(
          ".el-card__body"
        ).clientHeight;
        // 获取搜索区域的高度，+20为margin
        const searchHeight = _this.$refs[tableWrapperDom].clientHeight + 40;
        // 减去为表格所占高度，-50为距底边留白区域
        _this.tableMaxHeight = _this.elCardBodyHeight - searchHeight - 55;
        _this.tableMaxHeightFixed = _this.tableMaxHeight;
      });
    };
    Vue.prototype.tableHeightWatchSearch = function(
      _this,
      advancedSearchFormWrapper
    ) {
      _this.tableMaxHeight = 75;
      // 搜索区域高度改变式表格跟随改变
      _this.$nextTick(() => {
        // 获取扩展搜索区域的高度
        const searchForm = _this.$refs[advancedSearchFormWrapper].clientHeight;
        // 减去为表格所占高度，-30为距底边留白区域
        _this.tableMaxHeight = _this.elCardBodyHeight - searchForm - 110;
        _this.tableMaxHeightFixed = _this.tableMaxHeight;
      });
    };
    Vue.prototype.afterPrint = function(_this) {
      _this.fixed = true;
      window._that = _this;
      window.addEventListener("afterprint", removePrint, true);
    };
    Vue.prototype.removePrint = function(_this) {
      _this.fixed = true;
      window._that = _this;
      window.removeEventListener("afterprint", removePrint, true);
    };

    const userInfo = store.state.user.userDetal;
    const date = new Date();
    let formatDate = dateFormat("YYYY-mm-dd", date);
    Vue.prototype.userInfo = "";
    if (userInfo) {
      Vue.prototype.userInfo = `制表单位/制表人/日期：${userInfo.orgName}/${userInfo.userName}   ${formatDate}`;
    }

    Vue.prototype.formatJson = function(filterVal, jsonData) {
      return jsonData.map(v =>
        filterVal.map(j => {
          return v[j];
        })
      );
    };
    Vue.prototype.userHtml = function(a) {
      a = a + "";
      if (a) {
        return a.replace(/&nbsp;/g, " ");
      }
    };

    // 表单验证出错时定位到出错位置的方法
    Vue.prototype.errorLocation = _this => {
      _this.$nextTick(() => {
        const ele = _this.$el.querySelector(".is-error");
        if (ele) {
          // 获取到所在Row的offsetTop值
          window.scrollTo(0, ele.offsetTop - 100);
        }
      });
    };
  }
};
// 打印前执行
window.addEventListener("beforeprint", () => {
  const acfun = document.getElementsByClassName("el-table__header-wrapper")[0];
  acfun.style.display = "none";
});
// window.addEventListener("popstate", function(e) {
//   //  console.log(window.location.href);
//   // alert(window.location.href);

//   if (window.location.href.includes("appActivation")) {
//     router.go(-2);
//     console.log(router);
//   }
// });
// 单独写一个function 作用主要用于取消监听打印事件
function removePrint() {
  window._that.fixed = true;
  window._that.tableMaxHeight = window._that.tableMaxHeightFixed;
  const acfun = document.getElementsByClassName("el-table__body")[0];
  if (!acfun.childNodes[2]) return false;
  acfun.removeChild(acfun.childNodes[2]);
  const acfunHeader = document.getElementsByClassName(
    "el-table__header-wrapper"
  )[0];
  acfunHeader.style.display = "";
  acfun.style.textAlign = "center";
}
